package org.springblade.modules.ehb.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.modules.ehb.dto.EhbEnterpriseFeatureDto;
import org.springblade.modules.ehb.dto.EhbEnterpriseLabelDto;
import org.springblade.modules.ehb.service.*;
import org.springblade.modules.ehb.vo.*;
import org.springblade.modules.ehb.wrapper.EhbPolicyDrawWrapper;
import org.springblade.modules.ehb.wrapper.EhbPolicyWrapper;
import org.springblade.modules.enterprise.entity.Enterprise;
import org.springblade.modules.enterprise.service.IEnterpriseService;
import org.springblade.modules.policy.dto.EhbCashAcceptDTO;
import org.springblade.modules.policy.entity.MatMatter;
import org.springblade.modules.policy.service.IMatMatterService;
import org.springblade.modules.system.entity.AppLog;
import org.springblade.modules.system.entity.File;
import org.springblade.modules.system.service.IAppLogService;
import org.springblade.modules.system.service.IFileService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/ehb/policy")
public class EhbPolicyController {

	private IAppLogService appLogService;
	private IFileService fileService;
	private IMatMatterService matMatterService;
	private IEnterpriseService enterpriseService;
	private IEhbDictService dictService;
	private IEhbCityService cityService;
	private IEhbPolicyService policyService;
	private IEhbPolicyDrawService policyDrawService;
	private IEhbAwardCorporateService awardCorporateService;

	@GetMapping("/webInterface/matterWeb/page")
	public R<IPage<EhbPolicyDrawVO>> matterWebPage(Query query, Integer status, Integer isDeclareMatter, String enterpriseNature, String suitLevel, String enterpriseScale, Character industryType, String enterpriseSpecial, String name) {
		IPage<EhbPolicyDrawVO> data = policyDrawService.selectPolicyDrawPage(Condition.getPage(query), status, isDeclareMatter, enterpriseNature, suitLevel, enterpriseScale, enterpriseSpecial, industryType, name);
		return R.data(data);
	}

	@GetMapping("/webInterface/policyWeb/page")
	public R<IPage<EhbPolicyVO>> policyWebPage(Query query, Integer policyType, Long levelId, Character industryType, String policyName) {
		IPage<EhbPolicyVO> data = policyService.selectPage(Condition.getPage(query), policyType, levelId, industryType, policyName);
		return R.data(data);
	}

	@GetMapping("/webInterface/policyWeb/count")
	public R<EhbPolicyCountVO> policyWebCount() {
		EhbPolicyCountVO data = new EhbPolicyCountVO();
		data.setPolityCount(policyService.countPolicy());
		data.setMatterCount(policyDrawService.countPolicyDraw());
		data.setSubsidyCost(BigDecimal.valueOf(awardCorporateService.countSubsidyCost() * 0.0001).setScale(2, RoundingMode.HALF_UP));
		return R.data(data);
	}

	@GetMapping("/awardcorporate/page")
	public R<IPage<EhbAwardCorporateVO>> awardCorporatePage(Query query, Integer status) {
		IPage<EhbAwardCorporateVO> data = awardCorporateService.selectAwardCorporatePage(Condition.getPage(query), status);
		return R.data(data);
	}

	@GetMapping("/getDictByType")
	public R<List<Map<String, String>>> getDictByType(@RequestParam(name = "type") String type) {

		List<Map<String, String>> data = new ArrayList<>();

		Map<String, String> map = new HashMap<>();
		map.put("", "全部");
		data.add(map);

		if ("levelId".equals(type)) {
			List<EhbCityVO> cities = cityService.cityTree();
			for (EhbCityVO item : cities) {
				map = new HashMap<>();
				map.put(item.getRegionCode(), item.getRegionName());
				data.add(map);
			}
		} else {
			EhbDictVO dictVO = dictService.getDictByType(type);
			if (dictVO != null && dictVO.getDictItemList().size() > 0) {
				for (EhbDictItemVO item : dictVO.getDictItemList()) {
					map = new HashMap<>();
					map.put(item.getValue(), item.getLabel());
					data.add(map);
				}
			}
		}
		return R.data(data);
	}

	@GetMapping("/policyLibrary/api/page")
	public R<IPage<EhbPolicyVO>> policyLibraryPge(Query query, Boolean isPrecisePush, String enterpriseSpecial) {
		IPage<EhbPolicyVO> data = policyService.selectPushPage(Condition.getPage(query), isPrecisePush, enterpriseSpecial);
		return R.data(data);
	}

	@GetMapping("/policyLibrary/api")
	public R<EhbPolicyVO> policyLibrary(@RequestParam(name = "id") Long id) {
		EhbPolicyVO data = policyService.getById(id);
		data = EhbPolicyWrapper.build().entityVO(data);
		return R.data(data);
	}

	@GetMapping("/matMatter/api")
	public R<EhbPolicyDrawVO> matMatter(@RequestParam(name = "id") Long id) {
		EhbPolicyDrawVO data = policyDrawService.getById(id);
		data = EhbPolicyDrawWrapper.build().entityVO(data);
		return R.data(data);
	}

	@GetMapping("/enterpriseUser/findMyLabels")
	public R<Map<String, Object>> findMyLabels(@RequestParam String entUnitCode) {
		Map<String, Object> map = enterpriseService.findMyLabels(entUnitCode);
		return R.data(map);
	}

	@PostMapping("/enterpriseUser/updateMyLabels")
	public R<Object> updateMyLabels(@RequestBody String labelDto) {
		EhbEnterpriseLabelDto dto = JSONUtil.toBean(labelDto, EhbEnterpriseLabelDto.class);
		if (enterpriseService.updateMyLabels(dto.getCreditCode(), dto.getEnterpriseLabel())) {
			return R.data(R.success("成功"));
		}
		return R.data(R.fail("保存失败，未找到企业信息"));
	}

	@GetMapping("/enterpriseUser/findEntFeature")
	public R<EhbEnterpriseFeatureDto> findEntFeature(@RequestParam String entUnitCode) {
		EhbEnterpriseFeatureDto dto = new EhbEnterpriseFeatureDto();
		Enterprise enterprise = enterpriseService.getEnterpriseByCode(entUnitCode);
		if(enterprise == null)
			return R.data(null);
		BeanUtil.copy(enterprise, dto);
		return R.data(dto);
	}

	@GetMapping("/enterpriseUser/findEntUserExchangeRecord")
	public R<IPage<MatMatter>> findEntUserExchangeRecord(Query query, String enterpriseCode) {
		MatMatter matMatter = new MatMatter();
		matMatter.setEnterpriseCode(enterpriseCode);
		IPage<MatMatter> data = matMatterService.page(Condition.getPage(query), Condition.getQueryWrapper(matMatter));
		return R.data(data);
	}

	@PostMapping("/matMatter/cashAccpet")
	public R<Object> cashAccept(@RequestBody String cashAccept) {
		EhbCashAcceptDTO cashAcceptDTO = JSONUtil.toBean(cashAccept, EhbCashAcceptDTO.class);
		return R.data(R.status(matMatterService.ehbCashAccept(cashAcceptDTO)));
	}

	@GetMapping("/file/download")
	public void fileDownload(@RequestParam("fileId") Long fileId, HttpServletResponse response) {
		fileService.downloadFileById(fileId, response);
	}

	@PostMapping("/file/upload")
	public R<Object> fileUpload(@RequestPart("file") MultipartFile file) {
		File upload = fileService.uploadFile(file);
		if (upload == null) {
			return R.status(false);
		} else {
			return R.data(upload);
		}
	}

	@PostMapping("/applog/log")
	public R<Object> logApp(@RequestBody String logDto) {
		AppLog log = JSONUtil.toBean(logDto, AppLog.class);
		appLogService.save(log);
		return R.success("成功");
	}
}
